package controller;

import databaseLayer.CustomerUpdate;
import databaseLayer.OfficerUpdate;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import userBean.UserIdentity;

/**
 *
 * @author Sam
 */
public class Refund extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        UserIdentity userIdentity = (UserIdentity)session.getAttribute("userIdentity");
        
        String action = request.getParameter("action");
        String address = "index.do";
        
        if (action != null && action.equals("refund")) {
            String recordID = request.getParameter("recordID");
            
            if ((!session.isNew() && userIdentity.getUserType().equals("RegisteredCustomer")) && (recordID != null && recordID != "")) {
                CustomerUpdate.requestRefund(recordID);

                address += "?action=refund";
            }
        }
        else if (action != null && action.equals("approveRefund")) {
            if (!session.isNew() && userIdentity.getUserType().equals("TicketingOfficer")) {
                String[] recordID = request.getParameterValues("toBeRefund");
                for (int i = 0; i < recordID.length; i++) {
                    System.out.println(recordID[i]);
                    OfficerUpdate.approiveRefund(recordID[i]);
                }
                address += "?action=approve";
            }
        }
        
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
